package com.android.djlmapp.wxapi;

import android.content.Intent;
import android.os.Bundle;
import android.support.constraint.ConstraintLayout;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.home.HomeActivity;
import com.android.djlmapp.application.MyApplication;
import com.android.djlmapp.base.BaseActivity;
import com.android.djlmapp.model.home.Info;
import com.android.djlmapp.tools.global.ToastUtils;
import com.android.readcard.dialog.MyUsualyDialog;
import com.google.gson.Gson;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.tsy.sdk.pay.weixin.WXPay;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 张鹏
 * 微信支付界面
 * 2017.2.9
 */
public class WeiXinPayActivity extends BaseActivity {

    @BindView(R.id.top_bac)
    ImageView topBac;
    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.top_notes)
    ImageView topNotes;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    @BindView(R.id.page_title_layout)
    ConstraintLayout pageTitleLayout;
    @BindView(R.id.ed_cz)
    EditText edCz;
    @BindView(R.id.btn_qrcz)
    Button btnQrcz;
    @BindView(R.id.activity_wei_xin_pay)
    LinearLayout activityWeiXinPay;

    private static final String APP_ID = "wxa705f1b0d9e614da";
    private String money;
    private MyUsualyDialog dialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wei_xin_pay);
        ButterKnife.bind(this);
        initView();
        initData();
        WXPay.init(WeiXinPayActivity.this, APP_ID);      //要在支付前调用

    }

    @Override
    protected void initView() {
        edCz.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
    }

    @Override
    protected void initData() {
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.GONE);
        tvName.setText("微信支付");
    }


    @OnClick({R.id.ll_top_back, R.id.btn_qrcz})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_top_back:
                finish();
                break;
            case R.id.btn_qrcz:
                btnQrcz.setEnabled(false);
                money = edCz.getText().toString().trim();

                if (!TextUtils.isEmpty(money)) {
                    // 加载进度条
                    dialog = new MyUsualyDialog(WeiXinPayActivity.this, R.layout.wait_item);
                    dialog.setCanceledOnTouchOutside(false);
                    dialog.show();
                    //生成预支付交易单
                    MyApplication.getInstance().getHttpTools().weiXinPay(money, "wxpay_app",
                            "2", new StringCallback() {
                                @Override
                                public void onSuccess(Response<String> response) {
                                    try {
                                        Gson gson = new Gson();
                                        Info info = gson.fromJson(response.body(), Info.class);
                                        if (info.getCode() < 1000) {
                                            dialog.dismiss();
                                            doWXPay(response.body());
                                        } else {
                                            dialog.dismiss();
                                            Toast.makeText(WeiXinPayActivity.this,
                                                    info.msg, Toast.LENGTH_SHORT).show();
                                        }
                                    } catch (Exception e) {
                                        dialog.dismiss();
                                        ToastUtils.showShortToast(WeiXinPayActivity.this, "数据解析异常");
                                    }
                                }

                                @Override
                                public void onError(Response<String> response) {
                                    super.onError(response);
                                    Toast.makeText(WeiXinPayActivity.this,
                                            "网络或服务器错误", Toast.LENGTH_SHORT).show();
                                }

                                @Override
                                public void onFinish() {
                                    super.onFinish();
                                    btnQrcz.setEnabled(true);
                                    dialog.dismiss();
                                }
                            });
                } else {
                    btnQrcz.setEnabled(true);
                    Toast.makeText(WeiXinPayActivity.this,
                            "请输入充值金额", Toast.LENGTH_SHORT).show();
                }
                break;
        }
    }


    /**
     * 微信支付
     *
     * @param pay_param 支付服务生成的支付参数
     */
    private void doWXPay(String pay_param) {

        WXPay.getInstance().doPay(pay_param, new WXPay.WXPayResultCallBack() {
            @Override
            public void onSuccess() {
                Toast.makeText(WeiXinPayActivity.this, "充值成功", Toast.LENGTH_SHORT).show();
                startActivity(new Intent(WeiXinPayActivity.this, HomeActivity.class));
                finish();
            }

            @Override
            public void onError(int error_code) {
                switch (error_code) {
                    case WXPay.NO_OR_LOW_WX:
                        Toast.makeText(WeiXinPayActivity.this, "未安装微信或微信版本过低", Toast.LENGTH_SHORT).show();
                        break;

                    case WXPay.ERROR_PAY_PARAM:
                        Toast.makeText(WeiXinPayActivity.this, "参数错误", Toast.LENGTH_SHORT).show();
                        break;

                    case WXPay.ERROR_PAY:
                        Toast.makeText(WeiXinPayActivity.this, "支付失败", Toast.LENGTH_SHORT).show();
                        break;
                }
            }

            @Override
            public void onCancel() {
                Toast.makeText(WeiXinPayActivity.this, "支付取消", Toast.LENGTH_SHORT).show();
            }
        });
    }
}
